package render;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
/*
 * This is a class which is based on the texture loading portion of the the quad_textured tutorial
 * at http://lwjgl.org/wiki/index.php?title=The_Quad_textured
 * 
 * extended to be able to load more than just 2 textures at once
 * but only goes from 0 to 7 on the kinds of textures
 */
public class Texture {

/*
 * loads textures
 * 
 */
	private int[] texId;
	private int textureSelected= 0;
	
	//private static final int maxTextures = 8;// the most textures possible to load
	
	public void setupTextures(String[] textureAddress, int texNum[] )
	{
			texId = new int[1];
		
			texId[0] = loadPNGTexture( textureAddress[0] , GL13.GL_TEXTURE0);
					
		
		
		
	}//end of setUpTexture
	
	
	private int loadPNGTexture(String fileName , int textureUnit)
	{
		ByteBuffer buffer =null;
		int texWidth = 0;
		int texHeight = 0;
		
		
			
			//open File as input stream
			InputStream in;
			
			try {
				in = new FileInputStream(fileName);
			
			
			//
			PNGDecoder decoder = new PNGDecoder(in);
			
			//Get the width and height of the texture
			
			texWidth = decoder.getWidth();
			texHeight = decoder.getHeight();
			
			buffer = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
			decoder.decode(buffer, decoder.getWidth()*4, Format.RGBA);
			//flip buffer for open gl use
			buffer.flip();
			//close input stream
			in.close();
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(-1);
			}
			//Create a new texture object and bind it
			int texId=GL11.glGenTextures();
			GL13.glActiveTexture(textureUnit);
			GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
			
			//All RGB bytes are aliggned and each is 1 byte
			GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
			
			//Upload and generate a mips map
			
			GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, texWidth, texHeight, 0,
					GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
			
			//Setup st coordinates
			
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
			
			//Setup what to do when the texture has to be scaled
			
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
	
			return texId;
	}


	/**
	 * @return the texId
	 */
	public int[] getTexId() {
		
		int[] id = new int[texId.length];
		//copy texture ids into a new int[]
		
		for(int index =0; index<texId.length; index++)
		{
			
			id[index]= texId[index];
			
		}
		
		return id;//return the copy of texId
	
	}


	/**
	 * @param texId the texId to set
	 */
	public void setTexId(int[] texId) {
		this.texId = texId;
	}


	/**
	 * @return the textureSelected
	 */
	public int getTextureSelected() {
		return textureSelected;
	}


	/**
	 * @param textureSelected the textureSelected to set
	 */
	public void setTextureSelected(int textureSelected) {
		this.textureSelected = textureSelected;
	}
	
}
